package com.demo.mapper;

import com.demo.dto.ClassesPageQueryDTO;
import com.demo.dto.CourseDTO;
import com.demo.dto.CoursePageQueryDTO;
import com.demo.pojo.Classes;
import com.demo.pojo.Course;
import com.demo.vo.CourseVO;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface CourseMapper {

    @Insert(" INSERT INTO sys_course(courseID, classID, courseName, year, term, hour) VALUES (#{courseID}, (SELECT classID FROM sys_classes WHERE className = #{className}),#{courseName},#{year},#{term},#{hour})")
    void addCourse(Course course);


    @Delete("delete from sys_course where courseID=#{courseID}")
    void delete(String courseID);

    @Update("UPDATE sys_course " +
            "SET classID = (SELECT classID FROM sys_classes WHERE className = #{className}), " +
            "courseName = #{courseName}, term = #{term} " +
            "WHERE courseID = #{courseID}")
    void update(CourseDTO courseDTO);



    @Select("<script>" +
            "SELECT c.courseID, c.classID, c.courseName, c.year, c.term, c.hour, " +
            "cl.className AS className " +
            "FROM sys_course c " +
            "LEFT JOIN sys_classes cl ON c.classID = cl.classID " +
            "WHERE 1=1 " +
            "<if test='courseName != null and courseName.trim() != \"\"'> " +
            "AND c.courseName LIKE CONCAT('%', #{courseName}, '%') " +
            "</if> " +
            "<if test='courseID != null and courseID.trim() != \"\"'> " +
            "AND c.courseID = #{courseID} " +
            "</if> " +
            "<if test='term != null and term.trim() != \"\"'> " +
            "AND c.term = #{term} " +
            "</if> " +
            "ORDER BY c.courseID " +
            "</script>")
    Page<CourseVO> pageQuery(CoursePageQueryDTO coursePageQueryDTO);


    @Select("select * from sys_course where courseName=#{courseName}")
    Course selectByCourseName(String courseName);

    @Select("select * from sys_course where id=#{courseId}")
    Course selectByCourse(String courseId);
}
